<!--
@fucntion: 资产耗材费用信息
-->
<template>
<div class="module-basic-AssetConsumables">
  <el-row style="height:100%;">
    <el-col style="height:100%;">
      <table-panel 
      v-show="SwitchingPoint"
      :name="'资产耗材费用信息'" 
      :columns="AssetConsumablesColumns" 
      :tableData="AssetConsumablesData" 
      :showRowNumber="false" 
      :multiSelection="true"
      :inputLabelSearchMode="true"
      :canCreate="authorities.create" 
      :canDeleteAll="authorities.DeleteAll" 
      :canEdit="authorities.edit" 
      :canDelete="authorities.delete"
      :canExport="authorities.export"
      :canImport="authorities.Import"
      :action="actionUrl"
      :importFields="importField"
      @table-upload="onUpload"
      :currentPage="AssetConsumablesCurrentPage" 
      :totalNum="AssetConsumablesTotalNum" 
      :canDownTemplate="authorities.DownTemplate"
      @page-change="onPageChange('AssetConsumables',$event)" 
      :pageSize="pageSize"
      @table-searching="onSearching('AssetConsumables',$event)"
      @table-create="onCreate('AssetConsumables',$event)" 
      @table-DeleteAll="onDeleteAll('AssetConsumables',$event)" 
      @table-edit="onEdit('AssetConsumables', $event)"
      @table-delete="onDelete('AssetConsumables',$event)" 
      @table-export="onExport('AssetConsumables',$event)"
      @table-downTemplate="canDownTemplate('AssetConsumables',$event)"
      @selection-change="onSelectionChange('AssetConsumables',$event)">
      </table-panel>
      <el-col v-show="!SwitchingPoint">
        <el-card style="background-color: #DCDFE6;"> 
          <span v-if="(Ddata)" style="font-weight:bold;">资产耗材费用信息添加</span>
          <span v-else style="font-weight:bold;">资产耗材费用信息修改</span>
          <el-button type="warning" plain size="mini" style="float:right" 
            @click="onConfirm('AssetConsumables')">
            <span v-if="(Ddata)" style="font-weight:bold;">确认</span>
            <span v-else style="font-weight:bold;">确认修改</span>
          </el-button> 
          <el-button v-if="(Ddata)" type="danger"  plain size="mini" style="float:right" 
            @click="DetailedData = {};">重置</el-button>
          <el-button plain size="mini" style="float:right" 
            @click="SwitchingPoint = !(SwitchingPoint);">返回
          </el-button> 
        </el-card>
        <el-card style="width: 100%;height:100%;">
          <el-card>
            <el-row :gutter="24">
              <el-col :span="6">
                  <el-select v-model="DetailedData.DWBH" placeholder="选择单位" filterable clearable @change="Refresh1()"> 
                    <el-option
                      v-for="(item,index) in XLdata.data_DWBH"
                      :key="index"
                      :label="item.data"
                      :value="item.value">
                      <span style="float: left">{{ item.data }}</span>
                      <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
                    </el-option>
                  </el-select>
              </el-col>
              <el-col :span="18">
              </el-col>
            </el-row>
          </el-card>
          <el-card>
            <el-table
              ref="singleTable"
              :data="tableData1"
              size="mini"
              border
              highlight-current-row
              @current-change="handleCurrentChange"
              height="250"
              style="width: 100%">
              <el-table-column
                property="SBFLBH"
                label="设备编号"
                width="120">
              </el-table-column>
              <el-table-column
                property="SBMC"
                label="设备名称">
              </el-table-column>
              <el-table-column
                property="ZBSJ"
                label="装备时间">
              </el-table-column>
              <el-table-column
                property="FZDD"
                label="放置地点">
              </el-table-column>
              <el-table-column
                property="GGXH"
                label="规格型号">
              </el-table-column>
            </el-table>
            <div style="margin-top: 20px">
              <el-button size="mini" @click="setCurrent()">取消选择</el-button>
            </div>
          </el-card>
          <el-card>
            <div class="module-basic-Choice">
              <el-row style="height:100%;">
                <el-col style="height:100%;">
                  <table-panel :name="'耗材信息'" 
                  :columns="ChoiceColumns" 
                  :tableData="ChoiceData" 
                  :showRowNumber="false" 
                  :multiSelection="true"
                  :inputLabelSearchMode="true"
                  :canCreate="authorities.create" 
                  :canEdit="authorities.edit" 
                  :canDelete="authorities.delete"
                  :currentPage="ChoiceCurrentPage" 
                  :totalNum="ChoiceTotalNum" 
                  @page-change="onPageChange2('Choice',$event)" 
                  :pageSize="pageSize2"
                  @table-searching="onSearching2('Choice',$event)"
                  @table-create="onCreate2('Choice',$event)" 
                  @table-edit="onEdit2('Choice', $event)"
                  @table-delete="onDelete2('Choice',$event)" 
                  @selection-change="onSelectionChange2('Choice',$event)">
                  </table-panel>
                </el-col>
              </el-row>
              <Choice-dlg 
              :ChoiceRecord="ChoiceRecord" 
              :ChoiceRules="ChoiceRules" 
              :ChoiceDlgVisible="ChoiceDlgVisible" 
              :hasResetBtn="true"
              @close="ChoiceDlgVisible=false" 
              @confirm="onConfirm2('Choice')" 
              @reset-password="onResetPassword2('Choice')">
              </Choice-dlg>
            </div> 
          </el-card>    
        </el-card>
      </el-col>
    </el-col>
  </el-row>
</div>
</template>

<script>
import TablePanel from "@/components/basic/TablePanel";//一个简单的增删查改表格
import {
  formModelInitFromTable,
  formRulesInitFromTable
} from "@/utils/formutils.js";//从表格列变换到表单的一些工具
import VideoDlg from "@/modules/AssetConsumables/Choice/Choice";//新增记录

//meta info
var AssetConsumables = "AssetConsumables"; // 表格名称
var Choice = "Choice"; // 表格名称
var columnsAttr = "Columns"; // 列
var dataAttr = "Data"; // 数据
var selectionAttr = "Selection"; // 选中
var dlgVisibleAttr = "DlgVisible"; // 对话框
var formAttr = "Form";
var recordAttr = "Record"; // 表单模型
var rulesAttr = "Rules"; // 表单验证
var curPageAttr = "CurrentPage";// 分页-当前页
var totalNumAttr = "TotalNum";// 分页-总数
var qParamAttr = "QueryParams";// 查询参数

// 采集器信息-列
var AssetConsumablesColumns = [
  {
    prop: "costId",
    label: "编号",
    outOfSearch:true,
    hidden: true,
  },
  {
    prop: "SBFLBH",
    label: "设备编号",
    outOfSearch:true,
    hidden: true,
  },
  {
    prop: "SBMC",
    label: "设备名称",
  },
  {
    prop: "medicalConsumableId",
    label: "耗材编号",
    outOfSearch:true,
    hidden: true,
  },
  {
    prop: "medicalConsumableName",
    label: "耗材名称",
  },
 {
    prop: "DWBH",
    label: "部门编号",
    outOfSearch:true,
    hidden: true,
  },
  {
    prop: "DWMC",
    label: "所属部门",
  },
  {
    prop: "number",
    label: "数量",
    outOfSearch:true,
  },
  {
    prop: "categoryName",
    label: "持续月份",
    outOfSearch:true,
  },
  {
    prop: "totalMoney",
    label: "总金额",
    outOfSearch:true,
  },
  {
    prop: "monthlyMoney",
    label: "均分月金额",
    outOfSearch:true,
  },
  {
    prop: "validity",
    label: "有效否",
    outOfSearch:true,
    hidden: true,
  },
  {
    prop: "notes",
    label: "备注",
    outOfSearch:true,
    hidden: true,
  },
];

// -列
var ChoiceColumns = [
  {
    prop: "medicalConsumableId",
    label: "编号",
    outOfSearch:true,
    hidden: true,
  },
  {
    prop: "medicalConsumableDefinedId",
    label: "自定编号",
    width:80,
  },
  {
    prop: "medicalConsumableName",
    label: "耗材名称",
    width:150,
  },
  {
    prop: "specifications",
    label: "规格型号",
  },
  {
    prop: "price",
    label: "单价",
    outOfSearch:true,
  },
  {
    prop: "unit",
    label: "单位",
    outOfSearch:true,
    width:60,
  },
  {
    prop: "productionPlace",
    label: "生产厂家",
    width:170,
  },
  {
    prop: "number",
    label: "数量",
    outOfSearch:true,
  },
  {
    prop: "categoryName",
    label: "持续月份",
    outOfSearch:true,
  },
  {
    prop: "totalMoney",
    label: "总金额",
    outOfSearch:true,
  },
  {
    prop: "monthlyMoney",
    label: "均分月金额",
    outOfSearch:true,
  },
];

//导入
var importField = [
   "编号",           
   "设备编号",               
   "设备名称",        
   "耗材编号",       
   "耗材名称",  
   "数量",              
   "持续月份",        
   "总金额",  
   "均分月金额",      
   "有效否",            
   "备注"
];
//导出
var exportField = [
   "编号",           
   "设备编号",               
   "设备名称",    
   "耗材编号",          
   "耗材名称",  
   "数量",              
   "持续月份",        
   "总金额",  
   "均分月金额",      
   "有效否",            
   "备注"
];

export default {
  name: "AssetConsumables",
  components: {
    "table-panel": TablePanel,
    "Choice-dlg":VideoDlg
  },
  methods: {
    setCurrent(row) {
        this.$refs.singleTable.setCurrentRow(row);
      },
    handleCurrentChange(val) {
        this.currentRow = val;
    },
    //刷新页面
    Refresh1() {
      var me = this;
      let url = "/static/data/EquipmentConsumables.json";// /jcJsxxb
      if(url !== "") {
        me.getRequest(url,this.DetailedData.DWBH).then(resp => {
            if(resp.status == 200) {
              this.tableData1 = resp.data.results;
            }
        });
      }
    },
    onSearching: function(tname, params) {//查询
      var me = this;
      let url = "/static/data/AssetConsumables.json";// /jcJsxxb   /static/data/AssetConsumables.json

      if(url !== "") {
        me[tname+qParamAttr] = {};
        me[tname+qParamAttr] = params;
        if (params.isAccurate) {
          me[tname+qParamAttr] = params.paramPairs;
        } else {
          me[tname+qParamAttr].keywords = params.query;
        }
        me.getRequest(url, me[tname+qParamAttr]).then(resp => {
            if(resp.status == 200) {
              me[tname+dataAttr] = resp.data.results;
              // me[tname+totalNumAttr] = resp.data.data.length;

              me[tname+totalNumAttr] = resp.data.total;// 分页-总数
              this.pageSize = resp.data.limit;//每页数量
              me[tname+curPageAttr] = resp.data.offset;//起始页    results
            }
        });
      }
    },
    onCreate: function(tname) {// 新增
      var me = this;
      me[tname + dlgVisibleAttr] = true;
      me[tname + recordAttr] = formModelInitFromTable(me[tname + columnsAttr]);
      this.DetailedData = {};
      this.Ddata = true;
      this.SwitchingPoint = !(this.SwitchingPoint);
    },
    onEdit: function(tname, row) {// 编辑
      var me = this;
      me[tname + dlgVisibleAttr] = true;
      me[tname + recordAttr] = row;
      this.Ddata = false;
      this.SwitchingPoint = !(this.SwitchingPoint);
      this.DetailedData = row
    },
    onDelete: function(tname, row) {// 删除
      var me = this;
      me.$confirm("确定删除这条记录吗?", "提示", {
          type: "warning"
      }).then(() => {
          me.deleteRequest(`/api/AssetConsumables/${row.costId}`).then((resp)=>{
            if(resp.status == 200) {
              me.$message.success("删除成功!");
              me.onSearching(AssetConsumables,{});// 重新加载表格数据
            }else{
              me.$message.error('删除失败！');
            }
          });
      }).catch(() => {});
    },
    onDeleteAll:function(tname, row){//全部删除
      var me = this;
      me.$confirm("确定删除全部记录吗?", "提示", {
          type: "warning"
      }).then(() => {
          me.deleteRequest(`/api`).then((resp)=>{
            if(resp.status == 200) {
              me.$message.success("删除成功!");
              me.onSearching(AssetConsumables,{});// 重新加载表格数据
            }else{
              me.$message.error('删除失败！');
            }
          });
      }).catch(() => {});
    },
    onSelectionChange: function(tname, data) {// 选中改变
      var me = this;
      // 缓存记录，按照编号，查询对应的选中权限节点数组
      me[tname + selectionAttr] = data;
    },
    onExport: function(tableName, params) {// 导出
      var me = this;
      const h = me.$createElement;
      me.$notify.info({
        title: "提示",
        message: h("i", { style: "color: teal" }, "正在导出数据，请稍等……")
      });
      me.exportRequest("/sbxxgl/export", {
        fields: JSON.stringify(exportField),//exportField
        sfbf: false
      }).then(resp => {
        if (resp.status === 200) {
          me.$notify.success({
            title: "提示",
            message: h("i", { style: "color: #67C23A" }, "数据导出成功")
          });
          downExcel(resp);
        } else {
          me.$notify.success({
            title: "提示",
            message: h("i", { style: "color: #F56C6C" }, "数据导出失败")
          });
        }
      });
    },
    onUpload:function(msg,flag){//导入
      var me = this;
      if(flag){
        me.currentPage = 1;
        me.onSearching('sbxxgl',{page:me.currentPage,size:me.pageSize});
      }
    },
    onDownTemplate:function(tableName,params){//模板下载
      var me = this;
      const h = me.$createElement;
      me.$notify.info({
        title: '提示',
        message: h('i', { style: 'color: teal'}, '正在下载模板，请稍等……')
      });
      me.downFileRequest('/excelTemplate/收费项目基本信息.xlsx').then(resp=>{
        me.$notify.success({
          title: '提示',
          message: h('i', { style: 'color: #67C23A'}, '模板下载成功')
        });
          fileDownload(resp.data,"收费项目基本信息.xlsx");
      });
    },
    onPageChange:function(tname,page){// 分页改变
      var me = this;
      let url = "/jcJsxxb";// /jcJsxxb

      if(url !== ""){
        me[tname+curPageAttr] = page;
        me[tname+qParamAttr]["page"] = page;
        me.getRequest(url, me[tname+qParamAttr]).then(resp => {
            if(resp.status == 200) {
              me[tname+dataAttr] = resp.data.data;
              me[tname+totalNumAttr] = resp.data.data.length;
            }
        });
      }

    },
    onConfirm: function(tname) {// 对话框-确认
      let me = this;
      // 发送请求 /jcJsxxb

      if(me[AssetConsumables + recordAttr].SFXMBH == ""){

        me.$confirm("确定新增记录吗?", "提示", {
          type: "warning"
        }).then(() => {
          me.postRequest("/api/AssetConsumables",me[AssetConsumables + recordAttr]).then(resp => {
            if (resp.status == 200) {
              me.$message.success("提交成功！");

              //最后，隐藏对话框
              me[tname + dlgVisibleAttr] = false;
              this.SwitchingPoint = !(this.SwitchingPoint);
              me.onSearching(AssetConsumables, {});// 重新查一遍
            } else {
              me.$message.error("服务器发生错误，代码：" + resp.status);
            }
          });
        }).catch(() => {});
      }else{

        me.$confirm("确定修改记录吗?", "提示", {
          type: "warning"
        }).then(() => {
          me.postRequest("/api/VideoModify",me[AssetConsumables + recordAttr]).then(resp => {
            if (resp.status == 200) {
              me.$message.success("提交成功！");

              //最后，隐藏对话框
              me[tname + dlgVisibleAttr] = false;
              this.SwitchingPoint = !(this.SwitchingPoint);
              me.onSearching(AssetConsumables, {});// 重新查一遍
            } else {
              me.$message.error("服务器发生错误，代码：" + resp.status);
            }
          });
        }).catch(() => {});
      }
    },
    onSearching2: function(tname, params) {//查询
      var me = this;
      let url = "/static/data/Choice.json";// /jcJsxxb   /static/data/EquipmentConsumables.json

      if(url !== "") {
        me[tname+qParamAttr] = {};
        me[tname+qParamAttr] = params;
        if (params.isAccurate) {
          me[tname+qParamAttr] = params.paramPairs;
        } else {
          me[tname+qParamAttr].keywords = params.query;
        }
        me.getRequest(url, me[tname+qParamAttr]).then(resp => {
            if(resp.status == 200) {
              me[tname+dataAttr] = resp.data.results;
              me[tname+totalNumAttr] = resp.data.total;// 分页-总数
              this.pageSize2 = resp.data.limit;//每页数量
              me[tname+curPageAttr] = resp.data.offset;//起始页
            }
        });
      }
    },
    onCreate2: function(tname) {// 新增
      var me = this;
      me[tname + recordAttr] = formModelInitFromTable(me[tname + columnsAttr]);
      
      me[tname + dlgVisibleAttr] = true;
    },
    onEdit2: function(tname, row) {// 编辑
      var me = this;
      me[tname + recordAttr] = row;
      me[tname + dlgVisibleAttr] = true;
    },
    onDelete2: function(tname, row) {// 删除
      var me = this;
      me.$confirm("确定删除这条记录吗?", "提示", {
          type: "warning"
      }).then(() => {
          me.deleteRequest(`/api/Choice/${row.medicalConsumableId}`).then((resp)=>{
            if(resp.status == 200) {
              me.$message.success("删除成功!");
              me.onSearching2(Choice,{});// 重新加载表格数据
            }else{
              me.$message.error('删除失败！');
            }
          });
      }).catch(() => {});
    },
    onSelectionChange2: function(tname, data) {// 选中改变
      var me = this;
      // 缓存记录，按照编号，查询对应的选中权限节点数组
      me[tname + selectionAttr] = data;
    },
    onPageChange2:function(tname,page){// 分页改变
      var me = this;
      let url = "/jcJsxxb";// /jcJsxxb

      if(url !== ""){
        me[tname+curPageAttr] = page;
        me[tname+qParamAttr]["page"] = page;
        me.getRequest(url, me[tname+qParamAttr]).then(resp => {
            if(resp.status == 200) {
              //me[tname+dataAttr] = resp.data.results;
              //me[tname+totalNumAttr] = resp.data.total;
              me[tname+dataAttr] = resp.data.data;
              me[tname+totalNumAttr] = resp.data.data.length;
            }
        });
      }

    },
    onConfirm2: function(tname) {// 对话框-确认
      let me = this;
      // 发送请求 /jcJsxxb

      var aData = new Date();

      if(me[Choice + recordAttr].medicalConsumableId == ""){
        me.postRequest("/api/Choice",me[Choice + recordAttr]).then(resp => {
          if (resp.status == 200) {
            me.$message.success("提交成功！");
            //最后，隐藏对话框
            me[tname + dlgVisibleAttr] = false;
            me.onSearching2(Choice, {});// 重新查一遍
          } else {
            me.$message.error("服务器发生错误，代码：" + resp.status);
          }
        });
      }else{
        me.postRequest("/api/VideoModify",me[Choice + recordAttr]).then(resp => {
          if (resp.status == 200) {
            me.$message.success("提交成功！");
            //最后，隐藏对话框
            me[tname + dlgVisibleAttr] = false;
            me.onSearching2(Choice, {});// 重新查一遍
          } else {
            me.$message.error("服务器发生错误，代码：" + resp.status);
          }
        });
      }
    },
  },
  data() {
    return {
      authorities:{
        create:true,
        DeleteAll:true,
        delete:true,
        edit:true,
        search:true,
        export:true,
        Import:true,
        DownTemplate:true,
      },// 权限控制
      [AssetConsumables + columnsAttr]: AssetConsumablesColumns, // 表格-列
      [AssetConsumables + dataAttr]: [], // 表格的数据
      [AssetConsumables + selectionAttr]: [], // 当前选中
      [AssetConsumables + dlgVisibleAttr]: false, // 对话框是否可见
      [AssetConsumables + recordAttr]: {}, // 表单模型
      [AssetConsumables + rulesAttr]: {},// 表单验证
      [AssetConsumables + curPageAttr]:1,// 当前页
      [AssetConsumables + totalNumAttr]:0,// 总数
      [AssetConsumables + qParamAttr]:{},// 查询参数
      pageSize:20,// 每页数量
      [Choice + columnsAttr]: ChoiceColumns, // 表格-列
      [Choice + dataAttr]: [], // 表格的数据
      [Choice + selectionAttr]: [], // 当前选中
      [Choice + dlgVisibleAttr]: false, // 对话框是否可见
      [Choice + recordAttr]: {}, // 表单模型
      [Choice + rulesAttr]: {},// 表单验证
      [Choice + curPageAttr]:1,// 当前页
      [Choice + totalNumAttr]:0,// 总数
      [Choice + qParamAttr]:{},// 查询参数
      pageSize2:20,// 每页数量

      importField:importField.join(","),//导入的字段

      SwitchingPoint:true,//表单列表之间-切换控制

      DetailedData:{},//存储表单中的数据

      Ddata:true,//控制表单头标题的显示内容

      actionUrl:"/sbxxgl/import",//导入的路径


      XLdata:{
      _DWBH_:'',
      data_DWBH:{},//所属部门下拉

      },

      tableData1: [],
      currentRow: null,

      rules: {
          number: [
            { required: true, message: '请输入数量', trigger: 'blur' },
          ],
          categoryName: [
            { required: true, message: '请输入持续月份', trigger: 'blur' },
          ],
          totalMoney: [
            { required: true, message: '请输入总金额', trigger: 'blur' },
          ],
        }
    };
  },
  created: function() {
    var me = this;
    // 添加权限控制
    // me.authorities = me.$route.meta.authorities;

    // 表单模型初始化
    me[AssetConsumables + recordAttr] = formModelInitFromTable(me[AssetConsumables + columnsAttr]);
    // 表单验证初始化
    me[AssetConsumables + rulesAttr] = formRulesInitFromTable(me[AssetConsumables + columnsAttr]);
        // 表单模型初始化
    me[Choice + recordAttr] = formModelInitFromTable(me[Choice + columnsAttr]);
    // 表单验证初始化
    me[Choice + rulesAttr] = formRulesInitFromTable(me[Choice + columnsAttr]);
    //部门下拉初始化 
    me.getRequest("/static/data/data_BMMC.json",{}).then(resp=>{
      if(resp.status == 200) {
        me.XLdata.data_DWBH = resp.data.data;
      }
    });
    
    me.onSearching(AssetConsumables, {});// 重新查一遍
    me.onSearching2(Choice, {});// 重新查一遍
    me.Refresh1()// 重新查一遍

  }
};
</script>

<style lang="scss">
.module-basic-AssetConsumables {
  height: 100%;
}
</style>